<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>reg-demo2</title>
  <style>
    .right {
      color: red;
    }
    .left {
      color: green;
    }
  </style>
</head>

<body>
  <input type="text">
  <span></span>
  <script>
    const reg = /^[a-zA-Z0-9-_]{6,16}$/
    const input = document.querySelector('input')
    const span = document.querySelector('span')
    // 当鼠标离开，判断内容是否正确
    input.addEventListener('blur',function(){
      if (reg.test(this.value)){
        span.className = 'left'
        span.innerHTML = '输入格式正确'
      } else {
        span.className = 'right'
        span.innerHTML = '输入格式错误'
      }
    })
  </script>
</body>
</html>